<?php

require_once ('youtubeparser.php');

define ( 'FEED_BASE', "http://gdata.youtube.com/feeds/api/standardfeeds" );

class Youtube{
	/** curl object */
	var $curl = null;
	var $feedBase;
	var $userBase = 'http://gdata.youtube.com/feeds/api/users/%s/uploads';
	var $idBase = 'http://gdata.youtube.com/feeds/api/videos/%s';
	var $searchBase = 'http://gdata.youtube.com/feeds/api/videos';
	var $feeds = array ( 
		'mostRecent' => 'most_recent',
		'topRated' => 'top_rated',
		'topFavorites' => 'top_favorites',
		'mostViewed' => 'most_viewed',
		'mostPopular' => 'most_popular',
		'mostRecent' => 'most_recent',
		'mostDiscussed' => 'most_discussed',
		'mostLinked' => 'most_linked',
		'mostResponded' => 'most_responded',
		'recentlyFeatured' => 'recently_featured',
		'watchOnMobile' => 'watch_on_mobile',
	);
	var $searchResults = null;

	function __construct(){
		$this->curl = curl_init();
		$this->feedBase = FEED_BASE;
	}
	function _setUrl( $url, $headers = null ){
		curl_setopt($this->curl, CURLOPT_URL, $url);
    		curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->curl, CURLOPT_TIMEOUT, 60);
		if ( !is_null ($headers ) )
			curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($this->curl, CURLOPT_USERAGENT, "PHP Youtube Client");
	}
	function _appendOptions ( $url, $options ){
		return $url.'?'.http_build_query( $options );
	}
	/**
		process request and return the data
	*/
	function _request(){
		return curl_exec($this->curl );
	}
	function _close(){
		curl_close($this->curl);
	}
	function requestStatus(){
		return curl_getinfo( $this->curl );
		
	}
	function setFeedLocale( $LOCALE ){
		$this->feedBase = FEED_BASE.'/'.$LOCALE;
	}
	/**
		get most recents videos from youtube
		
	*/
	function mostRecent(){
		$this->_setUrl( $this->feedBase.'/'.$this->feeds['mostRecent'] );
		$data = $this->_request();
		$parser = new YoutubeParser( $data );
		$videos = $parser->videos();
		return $videos;
	}
	/**
		find in youtube api
		@param what Is what feed you want to query @see feeds
		@param options \li time Is the time for your query... can be today, this_week, this_month, all_time ( default )
			       \li max-results
			       \li start-index
	*/
	function find( $what , $options = array() ){
		if ( !isset ( $this->feeds[$what] ) ) return;
		$url = $this->feedBase.'/'.$this->feeds[$what];
		if ( !empty($options) )
			$url = $this->_appendOptions( $url, $options );
		YoutubeBasics::debug('Query: ' . $url );
		$this->_setUrl( $url );
		$data = $this->_request();
		$parser = new YoutubeParser( $data );
		$videos = $parser->videos();
		//$this->_close();
		return $videos;
	}
	function findUser( $username,$options = array() ){
		$url = sprintf ( $this->userBase, $username );
		if ( !empty($options) )
			$url = $this->_appendOptions( $url, $options );
		YoutubeBasics::debug('Query: ' . $url );
		$this->_setUrl( $url );
		$data = $this->_request();
		$parser = new YoutubeParser( $data );
		$videos = $parser->videos();
		return $videos;
	
	}
	function findId ( $id ){
		$url = sprintf ( $this->idBase, $id );
		YoutubeBasics::debug('Query: ' . $url );
		$this->_setUrl( $url );
		$data = $this->_request();
		$parser = new YoutubeParser( $data );
		$videos = $parser->videos();
		return $videos;
	}

	function related( $id, $options = array() ){
		$url = sprintf ( $this->idBase, $id ).'/related';
		$url = $this->_appendOptions( $url, $options );
		$this->_setUrl( $url );
		$data = $this->_request();
		$parser = new YoutubeParser( $data );
		$videos = $parser->videos();
		return $videos;
	}
	function feeds(){
		return array_keys( $this->feeds );
	}

	function search ( $query , $options = array() ){
		$options['vq'] = $query;
		$url = $this-> _appendOptions ( $this->searchBase , $options);
		$this->_setUrl( $url );
		$data = $this->_request();
		$parser = new YoutubeParser( $data );
		$videos = $parser->videos();
		$this->searchResults = $parser->searchResults();
		return $videos;

	}
	function _mapVideoInfo( $info ){
		$info = split ( "=", $info );
		return array( $info[0] => $info[1] );
	}

	/**
		@return url to download video
		TODO: improve
	*/
	function download ( $id ){
		$getVideoInfo = "http://youtube.com/get_video_info?&video_id=".$id;
		$this->_setUrl( $getVideoInfo );
		$videoInfo = split ( "&" , $this->_request() );
		$videoInfo = array_map( array("Youtube", "_mapVideoInfo" ) , $videoInfo );
		$infoArray = array();
		foreach ( $videoInfo as $info ){
			$keys = array_keys( $info );
			$values = array_values($info);
			$infoArray[  $keys[0] ] = $values[0];
		}
		extract ( $infoArray );
		return ( "http://www.youtube.com/get_video?video_id=$id&t=".$token );
		
	}
};
 
?>